<template>
  <el-container style="height: 100%; min-width: 600px">
    <el-header>
      <el-row>
        <el-button type="success"
                   icon="el-icon-plus"
                   @click="centerDialogVisible = true"
                   circle></el-button>

      </el-row>

      <!-- 弹出添加对话框 -->
      <el-dialog title="添加商品"
                 :visible.sync="centerDialogVisible"
                 width="30%"
                 center>
        <!-- 内容 -->
        <el-form label-position='left'
                 label-width="80px"
                 :model="formLabelAlign">
          <el-form-item label="拥有者ID">
            <el-input v-model="formLabelAlign.user_id"></el-input>
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input v-model="formLabelAlign.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品信息">
            <el-input v-model="formLabelAlign.goods_msg"></el-input>
          </el-form-item>
          <el-form-item label="商品价格">
            <el-input v-model="formLabelAlign.goods_gold"></el-input>
          </el-form-item>
          <!-- 上传文件 -->
          <el-upload class="upload-demo"
                     ref="upload"
                     action="http://121.37.209.61:8888/goods/addGoods"
                     :data="formLabelAlign"
                     :limit=1
                     :on-preview="handlePreview"
                     :on-remove="handleRemove"
                     :on-success="response => uploadOK(response)"
                     :file-list="fileList"
                     :auto-upload="false">
            <el-button slot="trigger"
                       size="small"
                       type="primary">选取文件</el-button>
            <div slot="tip"
                 class="el-upload__tip">最多只能上传一个文件</div>
          </el-upload>
        </el-form>
        <!-- 内容 -->
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="addSwiper">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 弹出添加对话框 -->
    </el-header>
    <el-main>
      <!-- 表格 -->
      <el-table v-loading="tableLoading"
                type="index"
                align="center"
                :data="tableData"
                stripe
                style="width: 100%"
                height="100%">
        <el-table-column fixed
                         prop="goods_id"
                         label="商品ID"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column fixed
                         prop="user_id"
                         label="拥有者ID"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="goods_name"
                         label="商品名称"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="goods_msg"
                         label="商品描述"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="goods_gold"
                         label="商品价格"
                         width=""
                         align="center">
        </el-table-column>
        <el-table-column prop="goods_img"
                         label="商品图片"
                         width=""
                         align="center">
          <template slot-scope="scope">
            <img :src="tableData[scope.$index].goods_img"
                 alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="goods_deal"
                         label="商品上架"
                         width=""
                         align="center">
          <template slot-scope="scope">
            <el-switch @change="switchChange(scope.$index, scope.row)"
                       v-model="tableData[scope.$index].goods_deal"
                       active-color="#13ce66"
                       inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right"
                         label="操作"
                         width="200"
                         align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       @click="handleEdit(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button size="mini"
                       type="danger"
                       @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- //弹出编辑框 -->
      <el-dialog title="修改商品"
                 :visible.sync="editDialogVisible"
                 width="30%"
                 center>
        <el-form label-position='left'
                 :model="form">

          <el-form-item label="商品ID"
                        :label-width="formLabelWidth">
            <el-input v-model="form.goods_id"
                      autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="拥有者ID"
                        :label-width="formLabelWidth">
            <el-input v-model="form.user_id"
                      autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="商品名称"
                        :label-width="formLabelWidth">
            <el-input v-model="form.goods_name"
                      autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="商品描述"
                        :label-width="formLabelWidth">
            <el-input v-model="form.goods_msg"
                      autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="商品价格"
                        :label-width="formLabelWidth">
            <el-input v-model="form.goods_gold"
                      autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer"
             class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="editDialogSubmit()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- //弹出编辑框 -->

    </el-main>
  </el-container>
</template>

<script>
import { HttpGet, HttpPost } from '@/api/http'
export default {
  name: "Goods",
  data () {
    return {
      centerDialogVisible: false,
      // 添加弹窗
      formLabelAlign: {
        user_id: '',
        goods_name: '',
        goods_msg: '',
        goods_gold: ''
      },
      fileList: [],
      tableData: [],
      tableLoading: true,
      editDialogVisible: false,
      form: {
        goods_id: '',
        user_id: '',
        goods_name: '',
        goods_msg: '',
        goods_gold: ''
      },
      formLabelWidth: '120px',
    };
  },
  created () {
    this.getTable()
  },
  methods: {
    //确认添加
    addSwiper () {
      this.centerDialogVisible = false
      this.submitUpload()
    },
    //上传文件
    submitUpload () {
      this.$refs.upload.submit();
    },
    //上传成功回调函数
    uploadOK (res) {
      if (res.code == 200) {
        this.getTable()
        this.fileList = []
        this.$message({
          message: '成功,上传记录成功！',
          type: 'success'
        });
      } else {
        this.$message.error('错误，上传记录失败！');
      }
    },
    handleRemove (file, fileList) {
      console.log(file, fileList);
    },
    handlePreview (file) {
      console.log(file);
    },
    //上下架
    async switchChange (index, row) {
      let goods_deal = row.goods_deal ? "1" : "0";
      let goods_id = row.goods_id;
      let res = await HttpGet({
        url: '/goods/deal',
        params: {
          goods_id: goods_id,
          goods_deal: goods_deal,
        }
      })
      try {
        if (res.data.code == 200) {
          goods_deal == '1' ?
            this.$message.success('商品上架成功') :
            this.$message.success('商品下架成功')
        } else {
          this.$message.error('网络请求失败')
        }
      }
      catch (error) {
        this.$message.error('服务器异常')
        console.log(error);
      }
    },
    // 删除行
    async handleDelete (index, rows) {
      try {
        let res = await HttpGet({
          url: '/goods/delete',
          params: {
            goods_id: rows.goods_id
          }
        })
        if (res.data.code == 200) {
          this.tableData.splice(index, 1)
          this.$message.success('成功,删除商品成功！')
        } else {
          that.$message.error('错误，删除商品失败！');
        }
      }
      catch (error) {
        console.log(error);
        that.$message.error('错误，服务器异常！');
      }
    },
    // 点击单个编辑
    handleEdit (index, row) {
      console.log(index, row);
      this.form = row
      this.editDialogVisible = true;
    },
    //编辑提交
    async editDialogSubmit () {
      try {
        let res = await HttpPost({ url: '/goods/update', data: this.form })
        if (res.data.code == 200) {
          this.getTable()
          this.editDialogVisible = false
          this.$message.success('成功,修改商品信息成功！')
        } else {
          this.$message.error('错误,修改商品信息失败！');
        }
      }
      catch (error) {
        console.log(error);
        this.$message.error('错误,修改商品信息失败！');
      }
    },
    async getTable () {
      this.tableLoading = true
      try {
        let res = await HttpGet({ url: '/goods/all' })
        if (res.data.code == 200) {
          this.tableData = res.data.data.map((item) => {
            item.goods_deal == 1
              ? (item.goods_deal = true)
              : (item.goods_deal = false);
            return item
          });
          this.tableLoading = false
        } else {
          this.$message.error('数据加载失败')
          this.tableLoading = false
        }
      }
      catch (error) {
        console.log(error);
        this.tableLoading = false
        this.$message.error('服务器异常')
      }
    }
  },

};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.el-header {
  padding-top: 20px;
  display: flex;
  justify-content: space-between;
}

.el-main {
  min-width: 600px;
  height: 100%;
  overflow: hidden;
}

/* 商品列表图片 */
.el-table img {
  width: 80px;
  height: 80px;
}
</style>
